/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package data.Imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import data.Services.UserActivationDAOService;
import model.entity.UserActivation;

/**
 *
 * @author quanhungbang
 */
public class UserActivationServiceDAOImp implements UserActivationDAOService{

    @Override
    public List<UserActivation> getAllUserActivation() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<UserActivation> getUserActivationsByEmail() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean createUserActivation(String key, int userID) {
        Connection cn = null;
        boolean flag = false;
        try {
            String strSql = "insert into user_activation(hashcode, user_id) values(?,?)";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            statement.setString(1, key);
            statement.setInt(2, userID);
            int row  = statement.executeUpdate();
            if(row > 0){
                flag = true;
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return flag;
    }

    @Override
    public boolean deleteUserActivation(String key) {
        Connection cn = null;
        boolean flag = false;
        try {
            String strSql = "DELETE FROM user_activation WHERE hashcode = ?;";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            statement.setString(1, key);
            int row = statement.executeUpdate();
            if(row >= 0){
                flag = true;
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return flag;
    }

    @Override
    public UserActivation getUserActivationByKeyUserID(String hashcode) {
        Connection cn = null;
        UserActivation ua = new UserActivation();
        try {
            String strSql = "select hashcode, user_id from user_activation where hashcode = ?";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            statement.setString(1, hashcode);
            ResultSet rs = statement.executeQuery();
            while(rs.next()){
                ua = new UserActivation(rs.getString("hashcode"), rs.getInt("user_id"));
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return ua;
    }
    
}
